﻿using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;

namespace PTop.Code.Xml
{
    public class SerializeHelper
    {
        public static T XmlDeserialize<T>(Stream inputSream) where T : class
        {
            try
            {
                var xz = new XmlSerializer(typeof(T));
                return (T)xz.Deserialize(inputSream);
            }
            catch
            {
                return default(T);
            }
        }

        public static T XmlDeserialize<T>(string inputXml, string encodeType = "UTF-8") where T : class
        {
            try
            {
                using (var ms = new MemoryStream())
                {
                    Encoding encode = Encoding.GetEncoding(encodeType);
                    using (var sr = new StreamWriter(ms, encode))
                    {
                        sr.Write(inputXml);
                        sr.Flush();
                        ms.Seek(0, SeekOrigin.Begin);
                        var serializer = new XmlSerializer(typeof(T));
                        return serializer.Deserialize(ms) as T;
                    }
                }
            }
            catch (Exception e)
            {
                return default(T);
            }
        }

        public static T[] XmlDeserializeArray<T>(string inputXml) where T : class
        {
            try
            {
                using (var sr = new StringReader(inputXml))
                {
                    var xz = new XmlSerializer(typeof(T));
                    return (T[])xz.Deserialize(sr);
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象</param>
        /// <returns></returns>
        public static string XmlSerialize<T>(T t) where T : class
        {
            using (var sw = new StringWriter())
            {
                var xs = new XmlSerializer(typeof(T));
                xs.Serialize(sw, t);
                return sw.ToString();
            }
        }

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象</param>
        /// <returns></returns>
        public static string XmlSerialize<T>(T[] t) where T : class
        {
            try
            {
                using (var sw = new StringWriter())
                {
                    var xs = new XmlSerializer(typeof(T));
                    xs.Serialize(sw, t);
                    return sw.ToString();
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 时间类型要求必须为yyyy-MM-dd hh:mm:ss格式
        /// </summary>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public static string ReplateDateTime(string inputStr)
        {
            var reg = new Regex(@"(?<yyyy>\d+)\/(?<MM>\d+)\/(?<dd>\d+) (?<HH>\d+):(?<mm>\d+):(?<ss>\d+)");
            var outStr = reg.Replace(inputStr, @"$1-$2-$3 $4:$5:$6");
            return outStr;
        }

    }
}
